package com.soft.admin.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.soft.admin.common.ElementConst;
import com.soft.admin.service.TopImgService;
import com.soft.dao.TopImgDao;
import com.soft.model.TopImg;
import com.soft.support.PageParam;
import com.soft.support.Pager;
import com.soft.util.DateUtil;
import com.soft.util.FileUtil;
import com.soft.util.JsonUtil;

@SuppressWarnings("unchecked")
@Component
public class TopImgServiceImpl extends BaseServiceImpl implements TopImgService {
	private TopImgDao topImgDao;
	
	@Autowired
	public void setTopImgDao(TopImgDao topImgDao) {
		this.topImgDao = topImgDao;
	}

	@Transactional(readOnly = true)
	public String get(TopImg params, PageParam page) {
		List<Criterion> values = new ArrayList<Criterion>();
		values.add(Restrictions.like("title", params.getTitle(), MatchMode.ANYWHERE));
		values.add(Restrictions.like("url", params.getUrl(), MatchMode.ANYWHERE));
		if (params.getState() != -1) {
			values.add(Restrictions.eq("state", params.getState()));
		}
		Pager actions = this.topImgDao.findForPager(TopImg.class, new Order[] { Order.desc("level"), Order.desc("id") },
				page.getStart(), page.getLimit(), values);
		return JsonUtil.toStringFromObject(actions);
	}

	@Transactional
	public String addOrEdit(HttpServletRequest request, TopImg params) {
		MultipartHttpServletRequest mulrequest = (MultipartHttpServletRequest)request;
		CommonsMultipartFile file = (CommonsMultipartFile)mulrequest.getFile("filepic");
		TopImg topImg = null;
		if (params.getId() == null) {
			topImg = new TopImg();
		} else {
			topImg = (TopImg)this.topImgDao.get(TopImg.class, params.getId());
		}
		if (file.getSize() > 0) {
			DiskFileItem fileItem = (DiskFileItem) file.getFileItem();
			String fileStr = fileItem.getName().substring(fileItem.getName().lastIndexOf(".")).toLowerCase();
			if (!".jpg".equals(fileStr) 
					&& !".png".equals(fileStr) 
					&& !".bmp".equals(fileStr) 
					&& !".gif".equals(fileStr)) {
				return JsonUtil.toRes("保存失败！", "图片格式错误");
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + fileStr;;
			String path = request.getSession().getServletContext().getRealPath(ElementConst.TOPIMGPATH);
			path = path + File.separator + fileName;
			File targetFile = new File(path);
			try {
				FileCopyUtils.copy(file.getBytes(), targetFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (topImg.getPic() !=null && !"".equals(topImg.getPic())) {
				FileUtil.delete(path + File.separator + topImg.getPic());
			}
			topImg.setPic(fileName);
		}
		topImg.setState(params.getState());
		topImg.setTitle(params.getTitle());
		topImg.setUrl(params.getUrl());
		topImg.setLevel(params.getLevel());
		this.topImgDao.addOrEdit(topImg);
		return JsonUtil.toRes("保存成功！");
	}

	@Transactional
	public String remove(HttpServletRequest request, Integer id) {
		TopImg topImg = (TopImg)this.topImgDao.get(TopImg.class, id);
		if (!"".equals(topImg.getPic())) {
			String path = request.getSession().getServletContext().getRealPath(ElementConst.TOPIMGPATH);
			FileUtil.delete(path + File.separator + topImg.getPic());
		}
		this.topImgDao.remove(topImg);
		return JsonUtil.toRes("删除成功！");
	}
}
